package com.fenghuajueli.libbasecoreui.feedback;

import android.os.Bundle;
import android.util.Pair;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.adapter.FeedbackTypeAdapter;
import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo;
import com.fenghuajueli.libbasecoreui.databinding.ActivityFeedbackPrevBinding;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.fenghuajueli.libbasecoreui.ui.CustomerServiceActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FeedbackPrevActivity extends BaseActivity {

    private ActivityFeedbackPrevBinding binding;
    private FeedbackTypeAdapter adapterType;

    private MutableLiveData<List<Pair<String, Integer>>> ldaFeedbackTypes;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFeedbackPrevBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initAdapter();
        initListener();
        initObserver();
        getFeedbackType();

    }

    private void initListener(){
        binding.imageback.setOnClickListener(v -> {
            finish();
        });
        binding.tvLxkf.setOnClickListener(v->{
            CustomerServiceActivity.start(this);
        });
    }

    private void initObserver(){
        ldaFeedbackTypes = new MutableLiveData<>();
        ldaFeedbackTypes.observe(this, new Observer<List<Pair<String, Integer>>>() {
            @Override
            public void onChanged(List<Pair<String, Integer>> list) {
                adapterType.resetData(list);
            }
        });
    }

    private void initAdapter(){
        adapterType = new FeedbackTypeAdapter(new ArrayList<>());
        binding.rvFeedbackType.setLayoutManager(new LinearLayoutManager(this));
        binding.rvFeedbackType.setAdapter(adapterType);
        adapterType.setItemListener(feedbackType -> {
            ThreeFeedbackActivity.start(this, feedbackType);
        });
    }

    /**
     * 获取反馈问题类型列表
     */
    private void getFeedbackType() {
        String url =  AppConfigInfo.getUrl() + "cms/feedbackType?app_id=" + AppConfigInfo.APPLICATION_ID;
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                List<Pair<String, Integer>> list = new ArrayList<>();
                list.add(new Pair<String, Integer>("Bug反馈", 1));
                ldaFeedbackTypes.postValue(list);
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                try {
                    String res = response.body().string();
                    JSONObject jobj = new JSONObject(res);
                    JSONObject data = jobj.optJSONObject("data");
                    JSONArray jArr = data.optJSONArray("list");
                    List<Pair<String, Integer>> list = new ArrayList<>();
                    for (int i = 0; i < jArr.length(); i ++) {
                        JSONObject obj = jArr.getJSONObject(i);
                        String typeStr = obj.optString("feedback_type");
                        int typeId = obj.optInt("id");
                        list.add(new Pair<String, Integer>(typeStr, typeId));
                    }
                    ldaFeedbackTypes.postValue(list);
                } catch (Throwable e) {
                    e.printStackTrace();
                    List<Pair<String, Integer>> list = new ArrayList<>();
                    list.add(new Pair<String, Integer>("Bug反馈", 1));
                    ldaFeedbackTypes.postValue(list);
                }
            }
        });
    }

}
